#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include "TcpClient.h"

#define INTERNAL_CMD_IP_ADDR      "127.0.0.1"
#define INTERNAL_TCP_PORT                9990
#define INTERNAL_TCP_SIZE                1024
#define INTERNAL_TCP_HANDLE_OK           "tcp_handle_ok"

typedef struct
{
    int len;
    char data[INTERNAL_TCP_SIZE - 4];
}STcpInfo,*pSTcpInfo;


pthread_mutex_t g_tcp_mutex;

int internal_tcp_creat_socket(void)
{
    int addrlen;
    int listen_socket;
    int client_socket;
    struct sockaddr_in addr;

    memset(&addr, 0, sizeof(addr));

    client_socket = socket(AF_INET, SOCK_STREAM, 0);
    if(client_socket == -1)
    {
        perror("JKSdkTcpClient: socket");
        return -1;
    }

    addr.sin_family = AF_INET;
    addr.sin_port = htons(INTERNAL_TCP_PORT);
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    inet_aton(INTERNAL_CMD_IP_ADDR, &(addr.sin_addr));

    addrlen = sizeof(addr);

again:
    listen_socket = connect(client_socket,(struct sockaddr *)&addr, addrlen);
    if(listen_socket == -1)
    {
        perror("JKSdkTcpClient: connect");
        usleep(100 * 1000);
        goto again;
    }

    return client_socket;
}

void * internal_tcp_thread(void * arg)
{
    int ret;
    int socket;
    fd_set fd;
    pSTcpInfo info;
    // SJKComSecondTimer timer;
    struct timeval timeout;

    info = (pSTcpInfo)arg;
    socket = internal_tcp_creat_socket();
    if(socket == -1)
    {
        goto exit;
    }

    send(socket,info,info->len + 4,MSG_NOSIGNAL);
    // Wrap_JKCom_second_timer(JKCOM_SET_NEW_TIME,&timer);

    while(1)
    {
        FD_ZERO(&fd);
        FD_SET(socket,&fd);

        timeout.tv_sec = 2;
        timeout.tv_usec = 0;

        ret = select(socket + 1,&fd,NULL,NULL,&timeout);
        if(ret > 0)
        {
            if(FD_ISSET(socket,&fd))
            {
                char buf[INTERNAL_TCP_SIZE];

                memset(buf,0,sizeof(buf));

                ret = recv(socket, buf,sizeof(buf),MSG_DONTWAIT);
                if(ret > 0)
                {
                    if(strcmp(buf,INTERNAL_TCP_HANDLE_OK) == 0)
                    {
                        printf("tcp handle ok\n");
                        break;
                    }
                }
            }
        }
        else if(ret == 0)
        {
            #if 0
            if(Wrap_JKCom_second_timer(JKCOM_NOT_SET_TIME,&timer) >= 18)
            {
                JK_PRT_WARN("tcp handle timeout\n");
                break;
            }
            #endif
        }
        else
        {
            printf("select error\n");
            break;
        }
    }

    close(socket);
exit:
    free(arg);
    pthread_mutex_unlock(&g_tcp_mutex);

    return NULL;
}

void Wrap_internal_tcp_send(char * buff)
{
    pthread_mutex_lock(&g_tcp_mutex);

    pthread_t id;
    pSTcpInfo info;

    info = (pSTcpInfo)malloc(INTERNAL_TCP_SIZE);
    memset(info,0,INTERNAL_TCP_SIZE);

    strcpy(info->data,buff);
    info->len = strlen(buff);

    pthread_create(&id, NULL,internal_tcp_thread, (void *)info);
    pthread_detach(id);
}

void Wrap_internal_tcp_init(void)
{
    pthread_mutex_init(&g_tcp_mutex, NULL);
    Wrap_internal_tcp_send("mkdir /mnt/config/product_func");
}

void Wrap_internal_tcp_destroy(void)
{
    pthread_mutex_destroy(&g_tcp_mutex);
}

#if DEG
int main(int agrc,char *argv[])
{
    char buf[24] = {0};

    sprintf(buf,"src_%s",argv[1]);

    Wrap_internal_tcp_send(buf);

    while (1)
    {
        sleep(5);
    }
    

    return 0;
}

#endif